<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「Android」使用VideoView和MediaController进行视频播放 | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[Android]使用VideoView和MediaController进行视频播放/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「Android」使用VideoView和MediaController进行视频播放</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/Android/">Android</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <p><img src="http://img.blog.csdn.net/20131121171540296?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHRkZXZz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 视频播放的activity&lt;br&gt;</div><div class="line"> * 使用方法：&lt;br&gt;</div><div class="line"> * 1、AndroidManifest.xml中添加此activity&lt;br&gt;</div><div class="line"> * 2、Intent中存入待播放视频的URL，支持本地和网络&lt;br&gt;</div><div class="line"> * 3、启动这个activity&lt;br&gt;</div><div class="line"> * &lt;br&gt;</div><div class="line"> * example：&lt;br&gt;</div><div class="line"> * String url = "play url";&lt;br&gt;</div><div class="line"> * Intent intent = new Intent();&lt;br&gt;</div><div class="line"> * intent.setClass(this, VideoPlayActivity.class);&lt;br&gt;</div><div class="line"> * intent.putExtra(VideoPlayActivity.KEY_URL, url);&lt;br&gt;</div><div class="line"> * startActivity(intent);&lt;br&gt;</div><div class="line"> *</div><div class="line"> * <span class="doctag">@author</span> ttworking</div><div class="line"> */</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">VideoPlayActivity</span> <span class="keyword">extends</span> <span class="title">Activity</span> <span class="keyword">implements</span> <span class="title">OnPreparedListener</span>, <span class="title">OnErrorListener</span> </span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String KEY_URL = <span class="string">"view_url"</span>;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"VideoPlayActivity"</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> PLAY_RETURN = <span class="number">2</span> * <span class="number">1000</span>; <span class="comment">// 2 seconds</span></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String KEY_PLAY_POSITON = <span class="string">"paly_position"</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TOAST_ERROR_URL = <span class="string">"Paly url is null, please check parameter:"</span> + KEY_URL;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TOAST_ERROR_PLAY = <span class="string">"Paly error, please check url exist!"</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String DIALOG_TITILE = <span class="string">"奋力加载中，请稍后..."</span>;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> String url;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> ProgressDialog progressDialog;</div><div class="line">    <span class="keyword">private</span> MediaController mc;</div><div class="line">    <span class="keyword">private</span> VideoView videoView;</div><div class="line">    <span class="keyword">private</span> LinearLayout llMain;</div><div class="line">    <span class="keyword">private</span> LayoutParams params = <span class="keyword">new</span> LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line"></div><div class="line">        url = getIntent().getStringExtra(KEY_URL);</div><div class="line">        <span class="keyword">if</span> (url == <span class="keyword">null</span> &amp;&amp; savedInstanceState != <span class="keyword">null</span>) &#123;</div><div class="line">            url = savedInstanceState.getString(KEY_URL);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">if</span> (url == <span class="keyword">null</span>) &#123;</div><div class="line">            Toast.makeText(getApplicationContext(), TOAST_ERROR_URL, Toast.LENGTH_LONG).show();</div><div class="line">            finish();</div><div class="line">            <span class="keyword">return</span>;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);</div><div class="line"></div><div class="line">        videoView = <span class="keyword">new</span> VideoView(<span class="keyword">this</span>);</div><div class="line">        videoView.setVideoURI(Uri.parse(url));</div><div class="line">        videoView.requestFocus();</div><div class="line">        videoView.setOnPreparedListener(<span class="keyword">this</span>);</div><div class="line">        videoView.setOnErrorListener(<span class="keyword">this</span>);</div><div class="line"></div><div class="line">        mc = <span class="keyword">new</span> MediaController(<span class="keyword">this</span>);</div><div class="line">        mc.setAnchorView(videoView);</div><div class="line">        mc.setKeepScreenOn(<span class="keyword">true</span>);</div><div class="line"></div><div class="line">        videoView.setMediaController(mc);</div><div class="line"></div><div class="line">        llMain = <span class="keyword">new</span> LinearLayout(<span class="keyword">this</span>);</div><div class="line">        llMain.setGravity(Gravity.CENTER_VERTICAL);</div><div class="line">        llMain.setOrientation(LinearLayout.VERTICAL);</div><div class="line">        llMain.setLayoutParams(params);</div><div class="line"></div><div class="line">        llMain.addView(videoView, params);</div><div class="line">        setContentView(llMain);</div><div class="line"></div><div class="line">        initDialog();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onSaveInstanceState</span><span class="params">(Bundle outState)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onSaveInstanceState(outState);</div><div class="line"></div><div class="line">        <span class="keyword">int</span> palyPosition = videoView.getCurrentPosition();</div><div class="line">        <span class="keyword">if</span> (palyPosition &gt; PLAY_RETURN) &#123;</div><div class="line">            palyPosition -= PLAY_RETURN;</div><div class="line">        &#125;</div><div class="line">        outState.putInt(KEY_PLAY_POSITON, palyPosition);</div><div class="line">        outState.putString(KEY_URL, url);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onRestoreInstanceState</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onRestoreInstanceState(savedInstanceState);</div><div class="line"></div><div class="line">        videoView.seekTo(savedInstanceState.getInt(KEY_PLAY_POSITON));</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPrepared</span><span class="params">(MediaPlayer mp)</span> </span>&#123;</div><div class="line">        progressDialog.cancel();</div><div class="line"></div><div class="line">        videoView.start();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onError</span><span class="params">(MediaPlayer mp, <span class="keyword">int</span> what, <span class="keyword">int</span> extra)</span> </span>&#123;</div><div class="line">        Log.d(TAG, <span class="string">"onError:"</span> + url);</div><div class="line"></div><div class="line">        Toast.makeText(getApplicationContext(), TOAST_ERROR_PLAY + <span class="string">"\n"</span> + url, Toast.LENGTH_LONG).show();</div><div class="line">        progressDialog.cancel();</div><div class="line">        finish();</div><div class="line"></div><div class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initDialog</span><span class="params">()</span> </span>&#123;</div><div class="line">        progressDialog = <span class="keyword">new</span> ProgressDialog(<span class="keyword">this</span>);</div><div class="line">        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);</div><div class="line">        progressDialog.setMessage(DIALOG_TITILE);</div><div class="line">        progressDialog.setCancelable(<span class="keyword">false</span>);</div><div class="line">        progressDialog.show();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/ttdevs/ttdevs.github.io/common/images/logo.png" alt="Create by ttdevs"></p>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Android]从浏览器中打开我们的应用/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Android」从浏览器中打开我们的应用</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[Android]仿联系人列表上的字母索引/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「Android」仿联系人列表上的字母索引</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
